﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FalionForm
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApplication1"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:WpfApplication1;assembly=WpfApplication1"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:LabelCheckBox/>
    ///
    /// </summary>
    public class LabelCheckBox : CheckBox
    {
        static LabelCheckBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LabelCheckBox), new FrameworkPropertyMetadata(typeof(LabelCheckBox)));
        }
        public LabelCheckBox()
        {
            //this.Click += ContentClicked;
            this.PreviewMouseLeftButtonDown += ContentClicked;
            this.PreviewMouseLeftButtonUp += ReSet;
        }

        #region 属性定义
        public static readonly DependencyProperty CheckedContentProperty =
            DependencyProperty.Register(
                "CheckedContent", typeof(string), typeof(LabelCheckBox),
                new FrameworkPropertyMetadata(" "));
        public string CheckedContent
        {
            get { return (string)GetValue(CheckedContentProperty); }
            set { SetValue(CheckedContentProperty, value); }
        }
        public static readonly DependencyProperty UncheckedContentProperty =
            DependencyProperty.Register(
                "UncheckedContent", typeof(string), typeof(LabelCheckBox),
                new FrameworkPropertyMetadata(" "));
        public string UncheckedContent
        {
            get { return (string)GetValue(UncheckedContentProperty); }
            set { SetValue(UncheckedContentProperty, value); }
        }
        public static readonly DependencyProperty NullContentProperty =
            DependencyProperty.Register(
                "NullContent", typeof(string), typeof(LabelCheckBox),
                new FrameworkPropertyMetadata(" "));
        public string NullContent
        {
            get { return (string)GetValue(NullContentProperty); }
            set { SetValue(NullContentProperty, value); }
        }
        public static readonly DependencyProperty ReadAddressProperty =
            DependencyProperty.Register(
                "ReadAddress", typeof(string), typeof(LabelCheckBox),
                new FrameworkPropertyMetadata(""));
        public string ReadAddress
        {
            get { return (string)GetValue(ReadAddressProperty); }
            set { SetValue(ReadAddressProperty, value); }
        }
        public static readonly DependencyProperty IsVisibityProperty =
            DependencyProperty.Register("IsVisibityAddress", typeof(string), typeof(LabelCheckBox),
            new FrameworkPropertyMetadata(""));
        public string IsVisibityAddress
        {
            get { return (string)GetValue(IsVisibityProperty); }
            set { SetValue(IsVisibityProperty, value); }
        }

        #endregion
        #region 事件定义
        public static readonly RoutedEvent TriggeredEvent =
            EventManager.RegisterRoutedEvent("Triggered", RoutingStrategy.Bubble,
                  typeof(TriggeredEventHandler), typeof(LabelCheckBox));
        public delegate void TriggeredEventHandler(object sender, RoutedEventArgs e);
        public event TriggeredEventHandler Triggered
        {
            add { AddHandler(TriggeredEvent, value); }
            remove { RemoveHandler(TriggeredEvent, value); }
        }

        public static readonly RoutedEvent ReSetEvent =
            EventManager.RegisterRoutedEvent("ReSeted", RoutingStrategy.Bubble,
                  typeof(ReSetEventHandler), typeof(LabelCheckBox));
        public delegate void ReSetEventHandler(object sender, RoutedEventArgs e);
        public event ReSetEventHandler ReSeted
        {
            add { AddHandler(ReSetEvent, value); }
            remove { RemoveHandler(ReSetEvent, value); }
        }

        #endregion
        protected virtual void ContentClicked(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(TriggeredEvent, this));
        }
        protected virtual void ReSet(object sender, RoutedEventArgs e)
        {
            RaiseEvent(new RoutedEventArgs(ReSetEvent, this));
        }
    }
}
